package vn.edu.saigontech.ncms.web.system;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import vn.edu.saigontech.ncms.model.system.ReceiptPercentDiscountItem;
import vn.edu.saigontech.ncms.service.ReceiptPercentDiscountItemService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class DiscountItemAction extends BaseAction {
	private Long id;
	private double percent;
	private boolean active;
	private String name;
	private String notation;
	
	public String list() {
		Collection<ReceiptPercentDiscountItem> items = ReceiptPercentDiscountItemService.INSTANCE.findAll();
		List<ReceiptPercentDiscountItem> list = new ArrayList<ReceiptPercentDiscountItem>(items);
		Collections.sort(list, new Comparator<ReceiptPercentDiscountItem> () {
			@Override
			public int compare(ReceiptPercentDiscountItem discount1, ReceiptPercentDiscountItem discount2) {
				if(discount1.getPercent() < discount2.getPercent())
					return -1;
				else if(discount1.getPercent() > discount2.getPercent())
					return 1;
				return 0 ;
			}
		});
		REQUEST().setAttribute("discountItems", list);
		return SUCCESS;
	}
	
	public String showAdd() {
		return SUCCESS;
	}
	
	public String add() {
		ReceiptPercentDiscountItem item = new ReceiptPercentDiscountItem();
		item.setActive(true);
		item.setName(this.name);
		item.setPercent(this.percent / 100);
		item.setActive(true);
		item.setReceiptNotation(this.notation);
		item = ReceiptPercentDiscountItemService.INSTANCE.persist(item);
		if(Checker.isNotNull(item)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String delete() {
		if(ReceiptPercentDiscountItemService.INSTANCE.deleteById(this.id)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String showEdit() {
		ReceiptPercentDiscountItem item = ReceiptPercentDiscountItemService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("item", item);
		return SUCCESS;
	}
	
	public String edit() {
		ReceiptPercentDiscountItem item = ReceiptPercentDiscountItemService.INSTANCE.findById(this.id);
		item.setName(this.name);
		item.setPercent(this.percent / 100);
		item.setActive(this.active);
		item.setReceiptNotation(this.notation);
		item = ReceiptPercentDiscountItemService.INSTANCE.persist(item);
		if(Checker.isNotNull(item)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public double getPercent() {
		return percent;
	}

	public void setPercent(double percent) {
		this.percent = percent;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNotation() {
		return notation;
	}

	public void setNotation(String notation) {
		this.notation = notation;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
}